<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>菜单项管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单项管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="selectByCondition()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">添加菜单项</el-button>
            </div>
            <el-table
                    :data="tableData"
                    style="width: 100%;margin-bottom: 20px;"
                    row-key="id"
                    border
                    :tree-props="{children: 'children'}">
                <el-table-column prop="name" align="center" sortable label="菜单名称"></el-table-column>
                <el-table-column prop="linkUrl" label="访问路径" sortable align="center"></el-table-column>
                <el-table-column prop="path" label="路径级别" sortable align="center"></el-table-column>
                <el-table-column prop="parentMenuId" label="自关联Id" sortable align="center"></el-table-column>
                <el-table-column prop="priority" label="菜单级别" sortable align="center"></el-table-column>
                <el-table-column label="操作" sortable align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-sizes="pagination.pageSizes"
                        :page-size="pagination.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>


            <!-- 新增标签菜单项弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单项" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单项名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="路径级别" prop="path">
                                    <el-input placeholder="正整数" v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input placeholder="正整数" v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="选择主菜单" prop="MainName">
                                    <el-select v-model="formData.MainName" >
                                        <el-option v-for="(name,index) in nameList" :key="index"  :label="name" :value="name"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图标">
                                    <el-input v-model="formData.icon" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述">
                                    <el-input v-model="formData.attention" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单项" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="路径级别" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="选择主菜单" prop="MainName">
                                    <el-select v-model="formData.MainName">
                                        <el-option v-for="(name,index) in nameList" :key="index" :label="name" :value="name"></el-option>

                                    </el-select>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图标">
                                    <el-input v-model="formData.icon" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="注意事项">
                                    <el-input v-model="formData.attention" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit(formData.id)">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    let vue = new Vue({
        el: '#app',
        data: {
            nameList:[],
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                pageSizes: [10, 20, 30, 40, 50, 100, 200, 300, 400],
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
            rules: {//校验规则
                name: [{required: true, message: '菜单名称为必填项', trigger: 'blur'}],
                linkUrl: [{required: true, message: '访问路径为必填项', trigger: 'blur'}],
                path: [{required: true, message: '路径级别为必填项', trigger: 'blur'}],
                priority: [{required: true, message: '优先级为必填项', trigger: 'blur'}],
                parentMenuId: [{required: true, message: '关联Id为必填项', trigger: 'blur'}],
                level: [{required: true, message: '菜单级别为必填项', trigger: 'blur'}],
                MainName: [{required: true, message: '菜单为必填项', trigger: 'blur'}]
            },
            tableData: []
        },
        created() {
            this.findPage();
        },
        methods: {

            //条件查询
            selectByCondition() {
                this.pagination.currentPage = 1;
                this.findPage();
            },
            //编辑
            handleEdit(row) {

                this.$refs["dataAddForm"].validate(result => {

                    if (result) {

                        axios.post("/menu/handleEdit", this.formData).then(res => {
                            if (res.data.flag) {
                                this.$message.success(res.data.message);
                            } else {
                                this.$message.error(res.data.message);
                            }

                        }).finally(() => {
                            this.dialogFormVisible4Edit = false;
                            this.resetForm();
                            this.findPage();
                        }).catch(resp=>{
                            this.$message.error("您没有权限")
                        })
                    }
                    this.dialogFormVisible4Edit = true;
                })
            },
            //添加
            handleAdd() {
                this.$refs["dataAddForm"].validate(result => {
                    if (result) {
                        axios.post("/menu/handleAdd", this.formData).then(res => {
                            if (res.data.flag) {
                                this.$message.success(res.data.message);
                            } else {
                                this.$message.error(res.data.message);
                            }
                        }).finally(() => {
                            this.findPage();
                            this.dialogFormVisible = false;
                            this.resetForm();
                        }).catch(resp=>{
                            this.$message.error("您没有权限")
                        })
                    } else {
                        this.$message({
                            showClose: true,
                            message: '必填项不能为空!',
                            type: 'warning'
                        });
                        this.dialogFormVisible = true;
                    }
                });
            },
            //分页查询
            findPage() {
                //将查询条件封装成json对象
                let param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/menu/findPage", param).then(res => {
                    this.tableData = res.data.rows;
                    this.pagination.total = res.data.total;
                });
            },
            // 重置表单
            resetForm() {
                //重置输入框中的数据
                this.formData = {};
                //重置校验提示信息
                this.$refs["dataAddForm"].resetFields();
            },
            // 弹出添加窗口
            handleCreate() {
                this.dialogFormVisible=true
                //查询主菜单名称集合
                axios.get("/menu/findMainMenuName").then(resp=>{
                    if (resp.data.flag){
                        this.nameList=resp.data.data
                        //alert(resp.data.data)
                    } else{
                        this.$message.error(resp.data.message)
                    }
                })

                this.resetForm()


            },
            // 弹出编辑窗口
            handleUpdate(row) {
                axios.get("/menu/findMainMenuName").then(resp=>{
                    if (resp.data.flag){
                        this.nameList=resp.data.data
                        //alert(resp.data.data)
                    } else{
                        this.$message.error(resp.data.message)
                    }
                })

                this.dialogFormVisible4Edit = true;
                axios.get("/menu/handleUpdate/" + row.id).then(res => {
                    this.formData=res.data;
                });

            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            //切换页面数据数量
            handleSizeChange(pageSize) {
                this.pagination.pageSize = pageSize;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm('此操作将永久删除检查组, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    axios.get("/menu/handleDelete/" + row.id).then(res => {
                        if (res.data.flag) {
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            });
                        } else {
                            this.$message.error(res.data.message);
                        }
                    }).finally(() => {
                        //分页查询
                        this.findPage();
                    }).catch(resp=>{
                        this.$message.error("您没有权限")
                    })
                })
            }
        }
    })
</script>
</html>